<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自动更新宿舍数据</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            text-align: center;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-top: 50px;
        }
        h1 {
            color: #333;
            margin-bottom: 20px;
        }
        p {
            color: #666;
            margin-bottom: 30px;
        }
        .btn {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 12px 24px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 10px 2px;
            cursor: pointer;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        .btn:hover {
            background-color: #45a049;
        }
        .status {
            margin-top: 20px;
            padding: 15px;
            background-color: #f9f9f9;
            border-radius: 4px;
            text-align: left;
            font-family: monospace;
            white-space: pre-wrap;
            max-height: 300px;
            overflow-y: auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>宿舍数据自动更新</h1>
        <p>点击下方按钮，自动清除并重新生成符合要求的宿舍数据。完成后刷新应用页面查看效果。</p>
        <button class="btn" onclick="updateDormData()">更新宿舍数据</button>
        <button class="btn" onclick="window.location.href = 'http://localhost:4201/'">前往应用页面</button>
        <div class="status" id="statusLog">准备就绪...</div>
    </div>

    <script>
        function log(message) {
            const logElement = document.getElementById('statusLog');
            logElement.textContent += '\n' + new Date().toLocaleTimeString() + ': ' + message;
            logElement.scrollTop = logElement.scrollHeight;
        }

        function updateDormData() {
            log('开始更新宿舍数据...');
            
            try {
                // 清除现有宿舍数据
                localStorage.removeItem('dormitoryStorage');
                log('已清除现有宿舍数据');
                
                // 清除学生的宿舍关联
                const studentStorage = localStorage.getItem('studentStorage');
                if (studentStorage) {
                    const students = JSON.parse(studentStorage);
                    students.forEach(student => {
                        if (student.dormitoryId) {
                            student.dormitoryId = undefined;
                        }
                    });
                    localStorage.setItem('studentStorage', JSON.stringify(students));
                    log('已清除学生宿舍关联');
                }
                
                // 生成新的宿舍数据
                const dormitories = [];
                const currentTime = new Date().toISOString();
                let dormIdCounter = 1;
                
                // 1. 学四舍（女生宿舍）
                const xuesisheFloors = [
                    { floor: 1, notes: '新生宿舍' },
                    { floor: 2, notes: '元宇宙女生宿舍' },
                    { floor: 3, notes: '鸿蒙生态开发女生宿舍' },
                    { floor: 4, notes: '云计算女生宿舍' },
                    { floor: 5, notes: '数智传媒女生宿舍' },
                    { floor: 6, notes: '人工智能女生宿舍' }
                ];
                
                for (const floorInfo of xuesisheFloors) {
                    for (let roomNum = 1; roomNum <= 10; roomNum++) {
                        const roomNumber = `${floorInfo.floor}${roomNum.toString().padStart(2, '0')}`;
                        dormitories.push({
                            id: `dorm-${dormIdCounter++}`,
                            building: '学四舍',
                            roomNumber: roomNumber,
                            type: 'female',
                            capacity: 8,
                            currentOccupancy: 0,
                            students: [],
                            floor: floorInfo.floor,
                            status: 'active',
                            notes: floorInfo.notes,
                            createdAt: currentTime,
                            updatedAt: currentTime
                        });
                    }
                }
                
                // 2. 学三舍（元宇宙男生宿舍和鸿蒙生态开发男生宿舍）
                for (let floor = 1; floor <= 6; floor++) {
                    const notes = floor <= 3 ? '元宇宙男生宿舍' : '鸿蒙生态开发男生宿舍';
                    for (let roomNum = 1; roomNum <= 10; roomNum++) {
                        const roomNumber = `${floor}${roomNum.toString().padStart(2, '0')}`;
                        dormitories.push({
                            id: `dorm-${dormIdCounter++}`,
                            building: '学三舍',
                            roomNumber: roomNumber,
                            type: 'male',
                            capacity: 8,
                            currentOccupancy: 0,
                            students: [],
                            floor: floor,
                            status: 'active',
                            notes: notes,
                            createdAt: currentTime,
                            updatedAt: currentTime
                        });
                    }
                }
                
                // 3. 皇悦三期（人工智能和云计算男生宿舍）
                for (let floor = 1; floor <= 6; floor++) {
                    const notes = floor <= 3 ? '人工智能男生宿舍' : '云计算男生宿舍';
                    for (let roomNum = 1; roomNum <= 5; roomNum++) {
                        const roomNumber = `93${floor}${roomNum.toString().padStart(2, '0')}`;
                        dormitories.push({
                            id: `dorm-${dormIdCounter++}`,
                            building: '皇悦三期',
                            roomNumber: roomNumber,
                            type: 'male',
                            capacity: 8,
                            currentOccupancy: 0,
                            students: [],
                            floor: floor,
                            status: 'active',
                            notes: notes,
                            createdAt: currentTime,
                            updatedAt: currentTime
                        });
                    }
                }
                
                // 4. 皇悦一期（数智传媒男生宿舍）
                for (let floor = 1; floor <= 6; floor++) {
                    for (let roomNum = 1; roomNum <= 10; roomNum++) {
                        const roomNumber = `91${floor}${roomNum.toString().padStart(2, '0')}`;
                        dormitories.push({
                            id: `dorm-${dormIdCounter++}`,
                            building: '皇悦一期',
                            roomNumber: roomNumber,
                            type: 'male',
                            capacity: 8,
                            currentOccupancy: 0,
                            students: [],
                            floor: floor,
                            status: 'active',
                            notes: '数智传媒男生宿舍',
                            createdAt: currentTime,
                            updatedAt: currentTime
                        });
                    }
                }
                
                // 保存宿舍数据到localStorage
                localStorage.setItem('dormitoryStorage', JSON.stringify(dormitories));
                log(`宿舍数据生成完成！共创建 ${dormitories.length} 间宿舍`);
                log('数据详情：');
                log(`- 学四舍：${xuesisheFloors.length * 10} 间女生宿舍`);
                log('- 学三舍：60 间男生宿舍（元宇宙和鸿蒙生态开发）');
                log('- 皇悦三期：30 间男生宿舍（人工智能和云计算）');
                log('- 皇悦一期：60 间数智传媒男生宿舍');
                log('请刷新应用页面查看更新后的数据！');
                
            } catch (error) {
                log('错误：' + error.message);
            }
        }

        // 页面加载时自动执行数据更新
        window.onload = function() {
            log('页面已加载，点击按钮开始更新...');
        };
    </script>
</body>
</html>